/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009  Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.edu.ufcg.ccc.javalog.controller;

import br.edu.ufcg.ccc.javalog.Franchise;
import br.edu.ufcg.ccc.javalog.Headquarters;
import br.edu.ufcg.ccc.javalog.Validator;
import br.edu.ufcg.ccc.javalog.view.ViewUtil;

/**
 * Controller for Headquarter model object.
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 11/10/09
 */
public class HeadquartersController {
	private Headquarters headquarters;
	
	/**
	 * Constructs a controller for a headquarters model object. 
	 * @param headquarters headquarters model object to manipulate
	 */
	public HeadquartersController(Headquarters headquarters) {
		Validator.validateObject(headquarters, new IllegalArgumentException("null pointer to headquarters"));
		this.headquarters = headquarters;
	}
	
	/**
	 * Adds a franchise.
	 */
	public void addFranchise() {
		if(!headquarters.addFranchise(ViewUtil.getFranchise())) 
			System.out.println("Ja existe uma franquia nesse local!");
		else
			System.out.println("Franquia adicionada com sucesso!");
	}
	
	/**
	 * Lets the user examine a franchise.
	 */
	public void seeFranchise() {
		Franchise f2 = headquarters.getFranchise(ViewUtil.getAddress());
		if(f2 != null) { 
			System.out.printf("Franquia encontrada: %n%n");
			System.out.println(f2);
		}
		else { 
			System.out.println("Nao existe uma franquia nesse local!");
		}
	}

	/**
	 * Lets the user change the airport of a franchise.
	 */
	public void changeFranchiseAirport() {
		Franchise f5 = headquarters.getFranchise(ViewUtil.getAddress());
		if(f5 != null) { 
			System.out.printf("Franquia encontrada: %n%n");
			System.out.println(f5);
			f5.setHasAirport(ViewUtil.getYesOrNo("Tem aeroporto?(s/n): "));
			System.out.println("Franquia atualizada.");
		}
		else { 
			System.out.println("Nao existe uma franquia nesse local!");
		}
	}

	/**
	 * Lets the user change the address of a franchise.
	 */
	public void changeFranchiseAddress() {
		Franchise f4 = headquarters.getFranchise(ViewUtil.getAddress());
		if(f4 != null) { 
			System.out.printf("Franquia encontrada: %n%n");
			System.out.println(f4);
			System.out.println();
			System.out.println("Digite o novo endereco. ");
			try {
				f4.setAddress(ViewUtil.getAddress());
			} catch (Exception e) {
				assert false : "Unreachable, getAddress() always returns a valid address";
			}
			System.out.println("Franquia atualizada.");
		}
		else { 
			System.out.println("Nao existe uma franquia nesse local!");
		}
	}

	/**
	 * Changes the name of the franchise.
	 */
	public void changeFranchiseName() {
		Franchise f3 = headquarters.getFranchise(ViewUtil.getAddress());
		if(f3 != null) { 
			System.out.printf("Franquia encontrada: %n%n");
			System.out.println(f3);
			System.out.println();
			f3.setName(ViewUtil.getString("Digite o novo nome: "));
			
		}
		else { 
			System.out.println("Nao existe uma franquia nesse local!");
		}
	}
	
	/**
	 * Removes a franchise.
	 */
	public void removeFranchise() {
		if(headquarters.removeFranchise(ViewUtil.getAddress()) != null)
			System.out.println("Franquia removida com sucesso!");
		else 
			System.out.println("Nao existe uma franquia nesse local!");
	}
}
